package com.Lixiaoqian.Gulangyu

import android.Manifest
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import com.Lixiaoqian.Gulangyu.base.BaseActivity
import com.Lixiaoqian.Gulangyu.base.QianTechApp
import com.Lixiaoqian.Gulangyu.adapter.FragmentAdapter
import com.Lixiaoqian.Gulangyu.fragments.PersonFragment
import com.Lixiaoqian.Gulangyu.fragments.RouteFragment
import com.Lixiaoqian.Gulangyu.location.ILocation
import com.Lixiaoqian.Gulangyu.location.LocationClient
import com.Lixiaoqian.Gulangyu.location.PositionBean
import com.Lixiaoqian.Gulangyu.route_m.RouteDetailActivity
import com.Lixiaoqian.Gulangyu.utils.LogUtils
import com.Lixiaoqian.Gulangyu.utils.PermissionsChecker
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_base.*
import kotlinx.android.synthetic.main.layout_navi_bar.*

class MainActivity : BaseActivity(), View.OnClickListener {
    override fun onClick(view: View?) {
        when (view?.id) {
            R.id.wd_navi_left ->
                wd_viewpager.currentItem = 0
            R.id.wd_navi_right ->
                wd_viewpager.currentItem = 1
            R.id.wd_navi_center ->
                RouteDetailActivity.startActivity(this)
        }
    }

    /**
     * bar 右上角点击回调
     */
    override fun barRightEvent() {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        PermissionsChecker.checkAndOpenPermiss(this,Manifest.permission.ACCESS_FINE_LOCATION)
        initBar()
        initViewPager()
        initEvent()
        initPosition()
    }

    private fun initPosition() {
        val locationClient = LocationClient(this)
        locationClient.setLocationResult(object: ILocation.LocationResult{
            override fun callPositionData(positionBean: PositionBean) {
                val qianTechApp = this@MainActivity.application as QianTechApp
                qianTechApp.currentPosition=positionBean
                LogUtils.i(positionBean.toString())
            }
        })
        locationClient.startLoca()
    }

    private fun initBar() {
        wd_toolbar.setTitle("鼓浪AR行")
        wd_toolbar.isShowLeftAndRightImage(GONE, VISIBLE)
    }

    private fun initEvent() {
        wd_navi_center.setOnClickListener(this)
        wd_navi_left.setOnClickListener(this)
        wd_navi_right.setOnClickListener(this)
    }

    private fun initViewPager() {
        wd_viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                setSelectImage(position)
            }

        })
        val fragmets = ArrayList<Fragment>().apply {
            add(RouteFragment())
            add(PersonFragment())
        }
        wd_viewpager.adapter = FragmentAdapter(supportFragmentManager, fragmets)
    }

    private fun setSelectImage(position: Int) {

    }
}
